package org.inspiration.dudu.web.controller.user;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.inspiration.dudu.common.pojo.Result;
import org.inspiration.dudu.user.model.dto.UserInfoDTO;
import org.inspiration.dudu.user.model.vo.UserVO;
import org.inspiration.dudu.web.annotation.Loggable;
import org.inspiration.dudu.web.service.user.UserFacadeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/users")
@Tag(name = "用户管理接口") // Updated tag for clarity
@Slf4j
@Loggable
@RequiredArgsConstructor
public class UserController {

    private final UserFacadeService userFacadeService;

    /**
     * 查询所有用户
     * @return 所有用户信息列表
     */
    @GetMapping
    @Operation(summary = "获取所有用户信息") // Updated summary for clarity
    public Result<List<UserVO>> findAllUsers() {
        return Result.success(userFacadeService.findAllUsers());
    }

    /**
     * 根据id查询用户
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据用户ID获取用户信息") // Updated summary for clarity
    public Result<UserVO> getUserById(@PathVariable("id") Long id) {
        return Result.success(userFacadeService.getUserById(id));
    }

    /**
     * 根据token查询用户
     *
     * @return 当前用户信息
     */
    @GetMapping("/me")
    @Operation(summary = "获取当前用户信息") // Updated summary for clarity
    public Result<UserVO> getUserByToken() {
        UserVO userVO = userFacadeService.getUserByToken();
        return Result.success(userVO);
    }

    @PutMapping
    @Operation(summary = "更新用户信息") // Updated summary for clarity
    public Result<Void> updateUserById(@RequestBody UserInfoDTO userInfoDTO) {
        userFacadeService.updateUserById(userInfoDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    @Operation(summary = "上传用户头像") // No change needed
    public Result<Void> uploadAvatar(@PathVariable("id") Long id, @RequestParam("file") MultipartFile file) {
        userFacadeService.uploadAvatar(id, file);
        return Result.success();
    }

    @GetMapping("/is-online/{id}")
    @Operation(summary = "查询用户是否在线", description = "用户最后一次请求结束后5-6分钟不再发起请求则视为下线")
    public Result<Boolean> isOnline(@PathVariable("id") Long id) {
       Boolean isOnline =  userFacadeService.isOnline(id);
       return Result.success(isOnline);

    }
    @GetMapping("/online")
    @Operation(summary = "获取所有在线用户", description = "分页查询")
    public Result<List<UserVO>> getOnlineUsers(@RequestParam("pageNum") Integer pageNum,
                                               @RequestParam("pageSize") Integer pageSize) {
        List<UserVO> list = userFacadeService.getOnlineUsers(pageNum, pageSize);
        return Result.success(list);
    }

    @GetMapping("/online-count")
    @Operation(summary = "获取所有在线用户的数量")
    public Result<Long> onlineUserCount() {
        Long count = userFacadeService.onlineUserCount();
        return Result.success(count);
    }
}
